/*
    7. Write a program in C to delete first node of Singly Linked List.
    Test Data :
    Input the number of nodes : 3
    Input data for node 1 : 2
    Input data for node 2 : 3
    Input data for node 3 : 4
    Expected Output :

    Data entered in the list are :                                                                               
    Data = 2                                                                                                     
    Data = 3                                                                                                     
    Data = 4                                                                                                     
                                                                                                                
    Data of node 1 which is being deleted is :  2                                                                
                                                                                                                
    Data, after deletion of first node :                                                                         
    Data = 3                                                                                                     
    Data = 4
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List next;
} Node;

List createList(int n)
{
    List root = NULL;
    List newNode = NULL;
    List curNode = NULL;
    int i;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(newNode == NULL){
            return NULL;
        }
        newNode->next = NULL;
        printf("Input data for node %d : ", i);
        scanf("%d", &(newNode->x));
        if(root == NULL){
            root = newNode;
            curNode = root;
        }else{
            curNode->next = newNode;
            curNode = newNode;
        }
    }
    return root;
}

List deleteFirstNode(List root)
{
    List preNode = root;
    root = root->next;
    free(preNode);
    return root;
}

void printList(List list)
{
    if(list == NULL){
        printf("List is empty!\n");
        return;
    }
    for(; list != NULL; list = list->next){
        printf("Data = %d\n", list->x);
    }
}

int main(void)
{
    List root = NULL;
    int n, x;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createList(n);
    printf("\nData entered in the list are : \n");
    printList(root);
    printf("Data of node 1 which is being deleted is : %d\n", root->x);    
    root = deleteFirstNode(root);
    
    printf("Data, after deletion of first node : \n");
    printList(root);

    return 0;

}

